package cn.iocoder.yudao.module.trade.statistics.convert;

import cn.iocoder.yudao.framework.business.basic.pojo.bo.DataComparison;
import cn.iocoder.yudao.module.api.pay.statistics.dto.PayWalletSummaryRespDTO;
import cn.iocoder.yudao.module.api.trade.statistics.dto.TradeOrderAreaStatisticsRespDTO;
import cn.iocoder.yudao.module.trade.statistics.bo.AfterSaleSummaryRespBO;
import cn.iocoder.yudao.module.trade.statistics.bo.TradeOrderAreaStatisticsRespBO;
import cn.iocoder.yudao.module.trade.statistics.bo.TradeOrderSummaryRespBO;
import cn.iocoder.yudao.module.trade.statistics.bo.TradeSummaryRespBO;
import cn.iocoder.yudao.module.trade.statistics.dal.dataobject.TradeStatisticsDO;
import cn.iocoder.yudao.module.trade.statistics.vo.TradeSummaryRespVO;
import cn.iocoder.yudao.module.trade.statistics.vo.TradeTrendSummaryExcelVO;
import cn.iocoder.yudao.module.trade.statistics.vo.TradeTrendSummaryRespVO;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 交易统计 Convert
 *
 * @author owen
 */
@Mapper
public interface TradeStatisticsConvert {

    TradeStatisticsConvert INSTANCE = Mappers.getMapper(TradeStatisticsConvert.class);

    default DataComparison<TradeSummaryRespVO> convert(TradeSummaryRespBO yesterdayData,
                                                       TradeSummaryRespBO beforeYesterdayData,
                                                       TradeSummaryRespBO monthData,
                                                       TradeSummaryRespBO lastMonthData) {
        return convert(convert(yesterdayData, monthData), convert(beforeYesterdayData, lastMonthData));
    }


    default TradeSummaryRespVO convert(TradeSummaryRespBO yesterdayData, TradeSummaryRespBO monthData) {
        TradeSummaryRespVO respVO = new TradeSummaryRespVO();
        respVO.setYesterdayOrderCount(yesterdayData.getCount());
        respVO.setYesterdayPayPrice(yesterdayData.getSummary());
        respVO.setMonthOrderCount(monthData.getCount());
        respVO.setMonthPayPrice(monthData.getSummary());
        return respVO;
    }

    DataComparison<TradeSummaryRespVO> convert(TradeSummaryRespVO value, TradeSummaryRespVO reference);

    DataComparison<TradeTrendSummaryRespVO> convert(TradeTrendSummaryRespVO value,
                                                    TradeTrendSummaryRespVO reference);

    List<TradeTrendSummaryExcelVO> convertList02(List<TradeTrendSummaryRespVO> list);

    TradeStatisticsDO convert(LocalDateTime time, TradeOrderSummaryRespBO orderSummary,
                              AfterSaleSummaryRespBO afterSaleSummary, Integer brokerageSettlementPrice,
                              PayWalletSummaryRespDTO walletSummary);

    @IterableMapping(qualifiedByName = "convertToEntity")
    List<TradeTrendSummaryRespVO> convertList(List<TradeStatisticsDO> list);

    TradeTrendSummaryRespVO convertA(TradeStatisticsDO tradeStatistics);

    @Named("convertToEntity")
    default TradeTrendSummaryRespVO convert(TradeStatisticsDO tradeStatistics) {
        TradeTrendSummaryRespVO respVO = convertA(tradeStatistics);
        respVO.setDate(tradeStatistics.getTime().toLocalDate());
        // 营业额 = 产品支付金额 + 充值金额
        respVO.setTurnoverPrice(tradeStatistics.getOrderPayPrice() + tradeStatistics.getRechargePayPrice());
        // 支出金额 = 余额支付金额 + 支付佣金金额 + 产品退款金额
        respVO.setExpensePrice(tradeStatistics.getWalletPayPrice() + tradeStatistics.getBrokerageSettlementPrice() + tradeStatistics.getAfterSaleRefundPrice());
        return respVO;
    }

    TradeOrderAreaStatisticsRespDTO convert(TradeOrderAreaStatisticsRespBO bean);

    List<TradeOrderAreaStatisticsRespDTO> convertList3(List<TradeOrderAreaStatisticsRespBO> list);

}
